﻿using HCQ2WebAPI_Logic.BaseAPIController;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using HCQ2_Model.ViewModel.WebAPI;
using HCQ2WebAPI_Logic.Model;
using HCQ2_Common.Constant;
using HCQ2_Model.DocModel.APIModel;
using HCQ2_Model.ViewModel;
using HCQ2_Model;
using HCQ2_Model.DocModel;
using System;
using HCQ2_Common.Encrypt;
using HCQ2_Model.BaneUser.APP.Params;
using System.Web;
using HCQ2_Model.DocModel.APIResultModel;
using HCQ2_Model.VR.Params;
using HCQ2_Model.VR.Model;
using HCQ2_Model.VLine.Params;
using HCQ2UI_Helper.Session;
using HCQ2_Common;
using HCQ2_Model.VLine.InputDto;
using HCQ2_Model.VLine.OutputDto;
using HCQ2_Model.VLine.Result;

namespace HCQ2WebAPI_Logic.DocumentController
{
    /// <summary>
    ///  VLine客户端相关接口
    /// </summary>
    public class VLineClientAPIController : BaseWeiXinApiLogic
    {
        //****************************************************1.0.1 *******************************************
        #region ✔1.0 保存作品分享信息 + WebApiResultJsonModel SaveFileShare(VLineParam model)
        /// <summary>
        ///  保存作品分享信息
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveFileShare(VLineParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            if (user_id <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "分享用户信息异常~", null);
            if (model.file_id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception,"待分享作品id不能为空~", null);
            var query = operateContext.bllSession.T_DocumentInfo.FindSingle(o => o.file_id == model.file_id);
            if (query == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "作品不存在不能被分享~", null);
            query.share_num += 1;
            operateContext.bllSession.T_DocumentInfo.Modify(query, s => s.file_id == model.file_id, "share_num");
            T_VLineShare share = new T_VLineShare { user_guid = model.userid, file_id = model.file_id, share_date = DateTime.Now };
            operateContext.bllSession.T_VLineShare.Add(share);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "作品分享成功~", query.share_num);
        }
        #endregion

        #region ✔1.1 保存用户关注用户信息 + WebApiResultJsonModel SaveUserAttention(UserAttention model)
        /// <summary>
        ///  保存用户关注信息【仅限VLions】
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveUserAttention(UserAttention model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //关注用户
            T_User user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.userid));
            if(user ==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "关注用户信息不存在~", null);
            //被关注用户
            T_User uuser = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.uuid));
            if (uuser ==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "被关注用户信息不存在~", null);
            var query = operateContext.bllSession.T_VLineAttentionUser.FindSingle(s => s.user_guid.Equals(model.userid) && s.by_guid.Equals(model.uuid));
            if(query!=null && model.utype==0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "用户关注信息保存成功", null);
            if (model.utype == 0)
            {
                #region 关注
                user.focus_num += 1;
                uuser.fans_num += 1;
                if (query != null)
                {
                    query.by_date = DateTime.Now;
                    operateContext.bllSession.T_VLineAttentionUser.Modify(query, s => s.id == query.id, "by_date");
                }else
                {
                    operateContext.bllSession.T_VLineAttentionUser.Add(new T_VLineAttentionUser {
                        user_guid = model.userid,
                        by_guid = model.uuid,
                        by_date = DateTime.Now
                    });
                }
                #endregion
            }else
            {
                #region 取消关注
                if(query == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前用户并未被关注~", null);
                user.focus_num -= 1;
                uuser.fans_num -= 1;
                operateContext.bllSession.T_VLineAttentionUser.Delete(s => s.id == query.id);
                #endregion
            }
            operateContext.bllSession.T_User.Modify(user, s => s.user_id == user.user_id, "focus_num");
            operateContext.bllSession.T_User.Modify(uuser, s => s.user_id == uuser.user_id, "fans_num");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "用户关注信息保存成功~", null);
        }
        #endregion

        #region ✔1.2 保存用户点赞信息 + WebApiResultJsonModel SaveUserStar(UserAttention model)
        /// <summary>
        ///  保存用户点赞信息
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveUserStar(UserAttention model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            //点赞用户
            T_User user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.userid));
            if (user == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "点赞用户信息不存在~", null);
            //被点赞用户
            T_User uuser = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(model.uuid));
            if (uuser == null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "被点赞用户信息不存在~", null);
            var query = operateContext.bllSession.T_VLineUserUserStar.FindSingle(s => s.user_guid.Equals(model.userid) && s.star_guid.Equals(model.uuid));
            if(query!=null && model.utype==0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "用户点赞信息保存成功~", null);
            if (model.utype == 0)
            {
                #region 点赞
                user.star_num += 1;
                uuser.mark_star_num += 1;
                if (query != null)
                {
                    query.star_date = DateTime.Now;
                    operateContext.bllSession.T_VLineUserUserStar.Modify(query, s => s.id == query.id, "star_date");
                }
                else
                {
                    operateContext.bllSession.T_VLineUserUserStar.Add(new T_VLineUserUserStar
                    {
                        user_guid = model.userid,
                        star_guid = model.uuid,
                        star_date = DateTime.Now
                    });
                }
                #endregion
            }
            else
            {
                #region 取消点赞
                if (query == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前用户并未点赞过~", null);
                user.star_num -= 1;
                uuser.mark_star_num -= 1;
                operateContext.bllSession.T_VLineUserUserStar.Delete(s => s.id == query.id);
                #endregion
            }
            operateContext.bllSession.T_User.Modify(user, s => s.user_id == user.user_id, "star_num");
            operateContext.bllSession.T_User.Modify(uuser, s => s.user_id == uuser.user_id, "mark_star_num");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "用户点赞信息保存成功~", null);
        }
        #endregion

        #region ✔1.3 保存目录点赞信息 + WebApiResultJsonModel SaveFolderStar(FolderStarParam model)
        /// <summary>
        ///  保存目录点赞信息
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveFolderStar(FolderStarParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            if(model.folder_id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "被点赞目录id不能为空~", null);
            //点赞用户
            int mark = operateContext.bllSession.T_User.SelectCount(s => s.user_guid.Equals(model.userid));
            if (mark <=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "点赞用户信息异常~", null);
            //被点赞目录
            T_DocumentFolder folder = operateContext.bllSession.T_DocumentFolder.FindSingle(s => s.folder_id == model.folder_id);
            if (folder==null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "被点赞目录信息异常~", null);
            var query = operateContext.bllSession.T_VLineUserFolderStar.FindSingle(s => s.user_guid.Equals(model.userid) && s.folder_id == model.folder_id);
            if(query!=null)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "用户点赞目录信息保存成功~", null);
            if (model.utype == 0)
            {
                #region 点赞
                folder.star_num += 1;
                if (query != null)
                {
                    query.star_date = DateTime.Now;
                    operateContext.bllSession.T_VLineUserFolderStar.Modify(query, s => s.id == query.id, "star_date");
                }
                else
                {
                    operateContext.bllSession.T_VLineUserFolderStar.Add(new T_VLineUserFolderStar
                    {
                        user_guid = model.userid,
                        folder_id = model.folder_id,
                        star_date = DateTime.Now
                    });
                }
                #endregion
            }
            else
            {
                #region 取消点赞
                if (query == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "当前用户并未点赞过此目录~", null);
                folder.star_num -= 1;
                operateContext.bllSession.T_VLineUserFolderStar.Delete(s => s.id == query.id);
                #endregion
            }
            operateContext.bllSession.T_DocumentFolder.Modify(folder, s => s.folder_id == model.folder_id, "star_num");
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "用户点赞目录信息保存成功~", null);
        }
        #endregion

        //****************************************************2.0 VLines接口*******************************************************
        /// <summary>
        ///  获取登录用户信息
        /// </summary>
        /// <param name="guid"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        private string GetUserName(string guid, out int userId)
        {
            T_User user = operateContext.bllSession.T_User.FindSingle(s => s.user_guid.Equals(guid));
            userId = 0;
            if (user == null)
                return string.Empty;
            userId = user.user_id;
            return user.user_name;
        }
        /// <summary>
        ///  根据code获取已上架商品名
        /// </summary>
        /// <param name="fileCode"></param>
        /// <returns></returns>
        private T_DocumentInfo GetFileStatusName(string fileCode)
        {
            return operateContext.bllSession.T_DocumentInfo.FindSingle(s => s.file_code.Equals(fileCode) && s.file_status == 5);
        }
        #region ✔2.0 保存体验成绩 + WebApiResultJsonModel SaveExperScore(ExperScoreParam model)
        /// <summary>
        ///  保存体验成绩
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveExperScore(ExperScoreParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            try
            {
                //1.1 获取登录用户信息
                int user_id, file_id;
                string userName = this.GetUserName(model.userid, out user_id);
                if (user_id <= 0)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验用户数据不存在~", null);
                //1.2 验证课件
                T_DocumentInfo addFile = GetFileStatusName(model.file_code);
                if (addFile == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, $" {model.file_code}：课件代码在已上架商品中不存在！", null);
                else
                   file_id = addFile.file_id;
                //T_VLionsRecord record = operateContext.bllSession.T_VLionsRecord.FindSingle(s => s.file_id == file_id && s.user_id == user_id);
                T_VLionsRecord record = operateContext.bllSession.T_VLionsRecord.FindSingle(s => s.er_id==model.er_id);
                T_DocumentFolderRelation folderRel = operateContext.bllSession.T_DocumentFolderRelation.FindSingle(s => s.file_id == file_id);
                int mark = 1;
                if (record == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, $" 体验记录不存在！", null);
                #region 体验记录表
                if (record != null)
                {
                    record.user_name = userName;
                    record.file_name = addFile.file_name;
                    record.file_code = model.file_code;
                    //record.in_date = DateTime.Now;
                    //record.em_time = model.em_time;
                    record.score = model.score;
                    record.folder_name = model.folder_name;
                    record.folder_code = model.folder_code;
                    operateContext.bllSession.T_VLionsRecord.Modify(record, s => s.er_id == record.er_id, "user_name", "file_name", "file_code",  "score", "folder_name", "folder_code");
                    mark = 1;
                }
                //else
                //{
                //    record = new T_VLionsRecord
                //    {
                //        user_id = user_id,
                //        user_name = userName,
                //        file_id = file_id,
                //        file_name = addFile.file_name,
                //        file_code = model.file_code,
                //        in_date = DateTime.Now,
                //        em_time = model.em_time,
                //        score = model.score
                //    };
                //    mark = operateContext.bllSession.T_VLionsRecord.Add(record);
                //}
                #endregion
                if (mark > 0)
                {
                    int learnNum = 0;
                    //添加主表：针对用户统计
                    var recordMain = operateContext.bllSession.T_VRExperienceRecordMain.FindSingle(o => o.user_id == record.user_id && o.file_id == record.file_id);
                    //添加主表：针对作品统计
                    var workMain = operateContext.bllSession.T_VRExperienceWorkMain.FindSingle(o => o.file_id == record.file_id && o.folder_code.Equals(model.folder_code) && o.content.Equals(model.content));
                    #region 2.0 主表记录：用户【统计表】
                    if (recordMain != null)
                    {
                        if (model.status == 0)
                            recordMain.result_error += 1;
                        else
                            recordMain.result_ok += 1;
                        operateContext.bllSession.T_VRExperienceRecordMain.Modify(recordMain, s => s.ma_id == recordMain.ma_id, "result_ok", "result_error");
                    }
                    else
                    {
                        learnNum = 1;
                        operateContext.bllSession.T_VRExperienceRecordMain.Add(new T_VRExperienceRecordMain
                        {
                            folder_path = folderRel.folder_path,
                            user_id = record.user_id,
                            file_id = record.file_id,
                            folder_name = record.folder_name,
                            file_code = record.file_code,
                            folder_code = record.folder_code,
                            result_ok = (model.status == 1) ? 1 : 0,
                            result_error = (model.status == 0) ? 1 : 0
                        });
                    }
                    #endregion

                    #region 2.1 用户【统计表】子表【答题序号】
                    var mUser = operateContext.bllSession.T_VRExperienceRecordMainUser.FindSingle(s => s.user_id == record.user_id && s.file_id == record.file_id && s.topic_num.Equals(model.topic_num));
                    if (mUser != null)
                    {
                        #region 更新
                        if (model.status == 0)
                            mUser.result_error += 1;
                        else
                            mUser.result_ok += 1;
                        operateContext.bllSession.T_VRExperienceRecordMainUser.Modify(mUser, s => s.ma_id == mUser.ma_id, "result_ok", "result_error");
                        #endregion
                    }
                    else
                    {
                        #region 添加
                        operateContext.bllSession.T_VRExperienceRecordMainUser.Add(new T_VRExperienceRecordMainUser
                        {
                            folder_path = folderRel.folder_path,
                            user_id = record.user_id,
                            file_id = record.file_id,
                            folder_name = record.folder_name,
                            file_code = record.file_code,
                            folder_code = record.folder_code,
                            result_ok = (model.status == 1) ? 1 : 0,
                            result_error = (model.status == 0) ? 1 : 0,
                            topic_num = model.topic_num
                        });
                        #endregion
                    }
                    #endregion

                    #region 3.0 主表记录：作品【统计表】
                    //作品统计
                    if (workMain != null)
                    {
                        if (model.status == 0)
                            workMain.result_error += 1;
                        else
                            workMain.result_ok += 1;
                        workMain.learn_num += learnNum;//学习人数
                        workMain.num = model.num;
                        workMain.answer = model.answer;
                        workMain.parse = model.parse;
                        workMain.wm_time = model.wm_time;
                        operateContext.bllSession.T_VRExperienceWorkMain.Modify(workMain, s => s.wm_id == workMain.wm_id, "result_ok", "result_error", "learn_num", "num", "answer", "parse", "wm_time");
                    }
                    else
                    {
                        workMain = new T_VRExperienceWorkMain
                        {
                            file_id = record.file_id,
                            file_code = record.file_code,
                            folder_path = folderRel.folder_path,
                            learn_num = learnNum,//学习人数
                            result_ok = (model.status == 1) ? 1 : 0,
                            result_error = (model.status == 0) ? 1 : 0,
                            line_id = model.line_id,
                            teacher_id = model.teacher_id,
                            folder_code = model.folder_code,
                            folder_name = model.folder_name,
                            num = model.num,
                            content = model.content,
                            answer = model.answer,
                            parse = model.parse,
                            wm_time = model.wm_time
                        };
                        operateContext.bllSession.T_VRExperienceWorkMain.Add(workMain);
                    }

                    #endregion

                    #region 4.0 添加成绩详细记录
                    operateContext.bllSession.T_VLinesExperienceDetail.Add(new T_VLinesExperienceDetail
                    {
                        er_id = record.er_id,
                        wm_id = workMain.wm_id,
                        user_id = record.user_id,
                        sel_result = model.sel_result ?? model.selResult,
                        status = model.status,
                        score = model.score,
                        em_time = model.em_time,
                        topic_num = model.topic_num,
                        create_date = DateTime.Now
                    });
                    #endregion
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "体验记录添加成功~", "");
                }
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "体验记录添加失败~", "");
            }
            catch (Exception ex)
            {
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, ex.Message, "");
            }
        }
        #endregion

        #region ✔2.1 获取作品收藏排行 + WebApiResultJsonModel GetWorkFavoriteRank(FavoriteRankParam model)
        /// <summary>
        ///  获取作品收藏排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetWorkFavoriteRank(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetUserStarWorkRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取关注作品排行~", result);
        }
        #endregion

        #region ✔2.2 获取作品下载排行 + WebApiResultJsonModel GetWorkDownloadRank(FavoriteRankParam model)
        /// <summary>
        ///  获取作品下载排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetWorkDownloadRank(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetWorkDownloadRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取作品下载排行~", result);
        }
        #endregion

        #region ✔2.3 获取作品学习时长排行 + WebApiResultJsonModel GetWorkLearnRank(FavoriteRankParam model)
        /// <summary>
        ///  获取作品学习时长排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetWorkLearnRank(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetWorkLearnRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取作品学习排行~", result);
        }
        #endregion

        #region ✔2.4 获取用户学习时长排行 + WebApiResultJsonModel GetUserLearnRank(FavoriteRankParam model)
        /// <summary>
        ///  获取用户学习时长排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetUserLearnRank(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetUserLearnRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户学习排行~", result);
        }
        #endregion

        #region ✔2.5 获取用户答题正确率排行 + WebApiResultJsonModel GetUserAnswerRank(FavoriteRankParam model)
        /// <summary>
        ///  获取用户答题正确率排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetUserAnswerRank(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetUserOkAccuracyRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户学习正确率排行~", result);
        }
        #endregion

        #region ✔2.6 获取题目出错次数统计 + WebApiResultJsonModel GetAnswerErrorData(FavoriteRankParam model)
        /// <summary>
        ///  获取题目出错次数统计
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetAnswerErrorData(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetLinesErrorRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取错误题目排行~", result);
        }
        #endregion

        #region ✔2.7 获取关注目录排行 + WebApiResultJsonModel GetUserFolderRank(FavoriteRankParam model)
        /// <summary>
        ///  获取关注目录排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetUserFolderRank(FavoriteRankParam model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            var result = operateContext.bllSession.T_DocumentFolder.GetUserStarFolderRank(model, user_id);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取关注目录排行~", result);
        }
        #endregion

        //****************************************************3.0 VLines接口【0902需求】*******************************************************
        #region ✔3.0 获取体验成绩明细 + WebApiResultJsonModel GetExperScoreDetail(ExperLineParam model)
        /// <summary>
        ///  获取体验成绩明细
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetExperScoreDetail(ExperLineParam model)
        {
            List<ExperScoreResult> result = null;
            //获取全部
            if (string.IsNullOrEmpty(model.userid) && string.IsNullOrEmpty(model.file_code) && model.line_id <= 0)
            {
                result = operateContext.bllSession.T_VLinesExperienceDetail.GetExperScoreMainList(model);
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取体验成绩明细~", result);
            }
            if (string.IsNullOrEmpty(model.file_code) && model.line_id>0)
            {
                var lineFile = operateContext.bllSession.N_VRLineClassRoomFile.FindSingle(s => s.RmId == model.line_id);
                if (lineFile == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据课堂id未匹配到对应的作品，请确认model.user_id信息！", null);
                model.file_id = lineFile.FileId;
            }
            if(!string.IsNullOrEmpty(model.file_code))
            {
                var file = operateContext.bllSession.T_DocumentInfo.FindSingle(s => s.file_code.Equals(model.file_code));
                if (file == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据作品代码未匹配到对应的作品，请确认信息！", null);
                model.file_id = file.file_id;
            }
            if(!string.IsNullOrEmpty(model.userid))
                model.user_id= SysPermissSession.ChangeIdByGuid(model.userid);
            result = operateContext.bllSession.T_VLinesExperienceDetail.GetExperScoreMainList(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取体验成绩明细~", result);
        }
        #endregion

        #region ✔3.1 获取体验成绩统计 + WebApiResultJsonModel ExperLineParam(ExperLineParam model)
        /// <summary>
        ///  获取体验成绩统计
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel GetExperScoreStatist(ExperLineParam model)
        {
            if (string.IsNullOrEmpty(model.file_code) && model.line_id <= 0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "作品代码和课堂id不能同时为空！", null);
            if (string.IsNullOrEmpty(model.file_code))
            {
                var lineFile = operateContext.bllSession.N_VRLineClassRoomFile.FindSingle(s => s.RmId == model.line_id);
                if (lineFile == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据课堂id未匹配到对应的作品，请确认信息！", null);
                model.file_id = lineFile.FileId;
            }
            else
            {
                var file = operateContext.bllSession.T_DocumentInfo.FindSingle(s => s.file_code.Equals(model.file_code));
                if (file == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "根据作品代码未匹配到对应的作品，请确认信息！", null);
                model.file_id = file.file_id;
            }
            var result = operateContext.bllSession.T_VLinesExperienceDetail.GetExperScoreStatist(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取体验成绩统计~", result);
        }
        #endregion

        //****************************************************4.0 VLines接口【1020需求】*******************************************************
        #region ✔4.1新增作品体验记录 + WebApiResultJsonModel SaveVRecord(AddExperRecord model)
        /// <summary>
        ///  新增作品体验记录
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel SaveVRecord(AddExperRecord model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            try
            {
                //1.1 获取用户信息
                int user_id, file_id;
                string userName = this.GetUserName(model.userid, out user_id);
                if (user_id <= 0)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验用户数据不存在~", null);
                //1.2 验证课件
                T_DocumentInfo addFile = GetFileStatusName(model.file_code);
                if (addFile == null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, $" {model.file_code}：课件代码在已上架商品中不存在！", null);
                else
                    file_id = addFile.file_id;
                T_VLionsRecord record = new T_VLionsRecord
                {
                    user_id = user_id,
                    user_name = userName,
                    file_id = file_id,
                    file_name = addFile.file_name,
                    file_code = model.file_code,
                    folder_name = "",
                    feed_source = model.feed_source,
                    in_date = DateTime.Now,
                    em_time = 0,
                    score = 0
                };
                int mark = operateContext.bllSession.T_VLionsRecord.Add(record);
                if (mark > 0)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "体验记录添加成功~", record.er_id);
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, "体验记录添加失败~", "");
            }
            catch (Exception ex)
            {
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, ex.Message, "");
            }
        }
        #endregion

        #region ✔4.2 更新作品体验记录 + WebApiResultJsonModel ModifyVRecord(EditExperRecord model)
        /// <summary>
        ///  更新作品体验记录
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public WebApiResultJsonModel ModifyVRecord(EditExperRecord model)
        {
            if (model.id<=0)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验id不允许为空！", null);
            try
            {
                var result = operateContext.bllSession.T_VLionsRecord.FindSingle(s => s.er_id == model.id);
                if(result==null)
                    return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, "体验记录不存在！", null);
                DateTime outDate= DateTime.Now;
                result.out_date = outDate;
                TimeSpan ts = outDate.Subtract(result.in_date);
                result.em_time = (int)ts.TotalSeconds;
                operateContext.bllSession.T_VLionsRecord.Modify(result, s => s.er_id == model.id, "out_date", "em_time");
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "体验记录更新成功~","");
            }
            catch (Exception ex)
            {
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Error, ex.Message, "");
            }
        }
        #endregion

        #region ✔4.3 获取作品体验时长排行 + WebApiResultJsonModel GetProductionRank(VLionsRank model)
        /// <summary>
        ///  获取作品体验时长排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetProductionRank(VLionsRank model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            List<VLionsWorkRank> list = await operateContext.bllSession.T_VLionsRecord.GetProductionRank(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取作品体验时长排行~", list);
        }
        #endregion

        #region ✔4.4 获取用户体验时长排行 + WebApiResultJsonModel GetProductionRank(VLionsRank model)
        /// <summary>
        ///  获取用户体验时长排行
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserRank(VLionsRank model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            List<VLionsUserRank> list = await operateContext.bllSession.T_VLionsRecord.GetUserRank(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户体验时长排行~", list);
        }
        #endregion

        //****************************************************5.0 VLines接口【1102需求】*******************************************************
        #region ✔5.1 获取用户收藏目录数 + WebApiResultJsonModel GetUserCollectFolderNum(UserCollectFolder model)
        /// <summary>
        ///  获取用户收藏目录数
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserCollectFolderNum(UserCollectFolder model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            int num = await operateContext.bllSession.T_VLionsRecord.GetUserCollectFolderNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户收藏目录数~", num);
        }
        #endregion

        #region ✔5.2 获取用户参加的课堂数 + WebApiResultJsonModel GetUserStudyNum(UserClassRoom model)
        /// <summary>
        ///  获取用户参加的课堂数
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserStudyNum(UserClassRoom model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            int num = await operateContext.bllSession.T_VLionsRecord.GetUserStudyNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户参加的课堂数~", num);
        }
        #endregion

        #region ✔5.3 获取用户购买商品数 + WebApiResultJsonModel GetUserPayGoodNum(UserPayGoodNum model)
        /// <summary>
        ///  获取用户购买商品数
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserPayGoodNum(UserPayGoodNum model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            int num = await operateContext.bllSession.T_VLionsRecord.GetUserPayGoodNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户购买商品数~", num);
        }
        #endregion

        #region ✔5.4 获取用户下载商品数 + WebApiResultJsonModel GetUserDownGoodNum(UserPayGoodNum model)
        /// <summary>
        ///  获取用户下载商品数
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserDownGoodNum(UserPayGoodNum model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            int num = await operateContext.bllSession.T_VLionsRecord.GetUserDownGoodNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户下载商品数~", num);
        }
        #endregion

        #region ✔5.5 获取用户收藏的商品数 + WebApiResultJsonModel GetUserCollectGoodNum(UserPayGoodNum model)
        /// <summary>
        ///  获取用户收藏的商品数
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserCollectGoodNum(UserPayGoodNum model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            int num = await operateContext.bllSession.T_VLionsRecord.GetUserCollectGoodNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户收藏的商品数~", num);
        }
        #endregion

        #region ✔5.6 获取用户点赞的商品数 + WebApiResultJsonModel GetUserLikeGoodNum(UserPayGoodNum model)
        /// <summary>
        ///  获取用户点赞的商品数
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserLikeGoodNum(UserPayGoodNum model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            int num = await operateContext.bllSession.T_VLionsRecord.GetUserLikeGoodNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户点赞的商品数~", num);
        }
        #endregion

        #region ✔5.7 获取用户答题正确率 + WebApiResultJsonModel GetUserAnswerNum(UserAnswerRatio model)
        /// <summary>
        ///  获取用户答题正确率
        /// </summary>
        /// <param name="model">参数</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiResultJsonModel> GetUserAnswerNum(UserAnswerRatio model)
        {
            if (!ModelState.IsValid)
                return ApiResultJsonModel.RedirectWebApi(WebResultCode.Exception, getErrorMessage(ModelState), null);
            int user_id = SysPermissSession.ChangeIdByGuid(model.userid);
            model.user_id = user_id;
            List<UserAnswerRatioResult> list = await operateContext.bllSession.T_VLionsRecord.GetUserAnswerNum(model);
            return ApiResultJsonModel.RedirectWebApi(WebResultCode.Ok, "成功获取用户答题正确率~", list);
        }
        #endregion
    }
}